package com.dabay.web.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.axis2.AxisFault;

import javax.xml.namespace.QName;

import net.sf.json.JSONArray;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.codehaus.jackson.map.ObjectMapper;

import com.chinalife.yunwei.main;

public class NbwxUtil {

	private static final String endpoint1 = "http://www.nbhrss.gov.cn:8080/axis2/services/DocService?wsdl";
	private static final String endpoint2 = "http://www.nbhrss.gov.cn:8080/axis2/services/ListService?wsdl";

	/**
	 * 调用 docService 接口方法
	 * 
	 * @param endpoint
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String getModelResult_docService(String params, String methods)
			throws Exception {
		// 使用RPC方式调用WebService
		RPCServiceClient serviceClient = new RPCServiceClient();
		Options options = serviceClient.getOptions();
		options.setProperty(
				org.apache.axis2.transport.http.HTTPConstants.CHUNKED, false);
		// 指定调用WebService的URL
		EndpointReference targetEPR = new EndpointReference(endpoint1);
		options.setTo(targetEPR);
		// 指定方法的参数
		Object[] opAddEntryArgs = new Object[] { params };
		// 指定方法返回值的数据类型的Class对象
		Class[] classes = new Class[] { String.class };
		// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空
		QName opAddEntry = new QName("http://ws.apache.org/axis2", methods);
		// 调用方法并输出该方法的返回�?
		Object test = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,
				classes)[0];
		return test.toString();
	}

	/**
	 * 调用 listService 接口方法
	 * 
	 * @param endpoint
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String getModelResult_listService(String params,String methods) throws Exception {
		// 使用RPC方式调用WebService
		RPCServiceClient serviceClient = null;
		String result = null;
		try {
			serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			// 指定调用WebService的URL
			EndpointReference targetEPR = new EndpointReference(endpoint2);
			options.setTo(targetEPR);
			// 指定方法的参数值
			Object[] opAddEntryArgs = new Object[] {params};
			// 指定方法返回值的数据类型的Class对象
			Class[] classes = new Class[] { String.class };
			// 指定要调用的方法及WSDL文件的命名空间
			QName opAddEntry = new QName("http://ws.apache.org/axis2",methods);
			// 调用方法并输出该方法的返回值
			result = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,
					classes)[0].toString();
			serviceClient.cleanupTransport(); // 为了防止连接超时

			// System.out.println(serviceClient.invokeBlocking(opAddEntry,
			// opAddEntryArgs, classes)[0]);
		} catch (AxisFault e) {
			e.printStackTrace();
		}
		return result;
	}
	
	//有的服务器可能不要转码 本地需要转码的
	 public String encodeCHNStr(String str) {  
       /* try {  
        	if(str==null || str==""){
        		return "";
        	}
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");  
        } catch (Exception e) {  
            return "";  
        } */
		return str; 
	    }  
	 
	//原有的网络图片 改成读取本地的图片
		 public String getLoacalFilePath(String str) { 
		    String REQUEST_PATH ="/otherDir/";
		    String LOCAL_SAVE_PATH ="/usr/local/nbrs_image/imagefile/";
			if(str==null || str==""){
				return "";
			}
			String[] filePathArray = str.split("/");
			String filename = filePathArray[filePathArray.length-1];	
			String mulu_one = filePathArray[3];
			String mulu_two = filePathArray[4];
			String mulu_three = filePathArray[5];
			String mulu_four = filePathArray[6];
			String realLocalPath = LOCAL_SAVE_PATH+mulu_one+"/"+mulu_two+"/"+mulu_three+"/"+mulu_four;
			String returnPath = mulu_one+"/"+mulu_two+"/"+mulu_three+"/"+mulu_four;
			//如果本地不存在 就跳过 不处理了
			File checkFile =  new File(realLocalPath+"/"+filename);
			if(!checkFile.exists()){
				return str;
			}
			return REQUEST_PATH+returnPath+"/"+filename; 
		    }  
	public static void main(String[] args) throws Exception {
		String result = NbwxUtil.getModelResult_docService("283255", "getListByDocId");
		System.out.println("-------66666-------"+result);
	}

}
